using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Text;
using DirkRettschlag.Utils;

namespace Departure
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
#if DEBUG
            Debug.WriteLine("Starting: " + DateTime.Now.ToString());
#endif
            if (Platform.GetDeviceType() == "Smartphone")
            {
                MessageBox.Show("Departure is not yet designed for Smartphones.", "Departure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return;
            }

#if DEBUG
            Application.Run(new frmMain());
#else
            
            try
            {
                Application.Run(new frmMain());
            }
            catch (Exception ex)
            {
                string exception = ExceptionHandling.ConstructFullExceptionChainString(ex);
                string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                string filename = "Crash" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt";

                StringBuilder info = new StringBuilder();

                string OEMString = "";
                if (Registry.GetStringValue(Registry.HKLM, "\\System\\Platform", "OEMString", ref OEMString) == Registry.ERROR_SUCCESS)
                {
                    OEMString = OEMString.Substring(0, OEMString.IndexOf('\0'));
                }

                info.AppendLine("Model: " + OEMString);

                info.AppendLine("Type: " + Platform.GetDeviceType());

                info.AppendLine("OS: " + Environment.OSVersion.Platform.ToString() + " " + Environment.OSVersion.Version.ToString() + " (Build " + Environment.OSVersion.Version.Build.ToString() + "." + Platform.GetAKUVersion() + ")");

                info.AppendLine("Resolution: " + Screen.PrimaryScreen.Bounds.Height.ToString() + "x" + Screen.PrimaryScreen.Bounds.Width.ToString());
                
                Utilities.MemoryStatus internalReport = new Utilities.MemoryStatus();
                if (Utilities.GlobalMemoryStatusWinCE(internalReport))
                {
                    info.AppendLine("Physical Memory: " + internalReport.ullAvailPhys.ToString() + " / " + internalReport.ullTotalPhys.ToString());
                    info.AppendLine("Virtual Memory: " + internalReport.ullAvailVirtual.ToString() + " / " + internalReport.ullTotalVirtual.ToString());
                    info.AppendLine("PageFile: " + internalReport.ullAvailPageFile.ToString() + " / " + internalReport.ullTotalPageFile.ToString());
                }

                using (StreamWriter sw = new StreamWriter(appPath + "\\"+ filename))
                {
                    sw.Write(exception + "\r\n-----------------\r\n" + info.ToString());
                    sw.Flush();
                }

                Process p = new Process();
                p.StartInfo = new ProcessStartInfo(appPath + "\\CrashReporter.exe", filename);
                p.Start();
            }
#endif
        }
    }

}